package sanrenxing.dayEnd.backRepertoryTotal.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sanrenxing.dayEnd.backRepertoryTotal.dao.SecuritiesDao;
import sanrenxing.dayEnd.backRepertoryTotal.domain.ReceivableAndPayable;
import sanrenxing.dayEnd.backRepertoryTotal.service.SecuritiesService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @desrciption 证券应收应付 业务处理
 * @auth：yidu 李小锋
 * @date:2021/3/14 0014 - 9:52
 * @version:1:0
 */
@Service("SercuritiesService")
public class SecuritiesServiceImpl implements SecuritiesService {
    //自动装配
    @Autowired
    private SecuritiesDao securitiesDao;
    /**
     * 新增一行数据
     *
     * @param securities 对象
     * @return 0、增加失败 1、增加成功
     */
    @Override
    public int add(ReceivableAndPayable securities) {
        return securitiesDao.add(securities);
    }

    /**
     * 删除一行数据
     *
     * @param srapId 编号
     * @return 0、删除失败 1、删除成功
     */
    @Override
    public int delete(String srapId) {
        return securitiesDao.delete(srapId);
    }
    /**
     * 表示权益中的删除
     *
     * @param seId
     * @param businDate
     * @return
     */
    @Override
    public int EquityDateDelete(String seId, Date businDate) {
        return securitiesDao.EquityDateDelete(seId,businDate);
    }

    /**
     * 表示证券交易数据中的删除
     *
     * @param ceId
     * @param tranDate
     * @return
     */
    @Override
    public int TransactionDelete(String ceId, Date tranDate) {
        return securitiesDao.TransactionDelete(ceId,tranDate);
    }

    /**
     * 修改一行数据
     *
     * @param securities 对象
     * @return 0、修改失败 1、修改成功
     */
    @Override
    public int update(ReceivableAndPayable securities) {
        return securitiesDao.update(securities);
    }

    /**
     * 查询所有的订单数据
     *
     * @return 返回一个订单集合
     */
    @Override
    public List<ReceivableAndPayable> findAll() {
        return securitiesDao.findAll();
    }

    /**
     * 分页查询
     *
     * @param start     开始行号
     * @param rows      每页行数
     * @param condition 查询条件
     * @return 集合
     */
    @Override
    public List<ReceivableAndPayable> findByPage(int start, int rows, String condition) {
        int pages=((rows-1)*start)+1;
        return securitiesDao.findByPage(pages,start+pages,condition);
    }

    /**
     * 按条件统计出记录行数
     *
     * @param condition 条件
     * @return 记录行数
     */
    @Override
    public int count(String condition) {
        return securitiesDao.count(condition);
    }

    /**
     * 获取查询条件
     *
     * @param securities 对象
     * @return 条件字符串
     */
    @Override
    public String getcondition(ReceivableAndPayable securities) {
        String codition=" ";
        //判断证券对象是否等于空
        if(securities==null){
            return codition;
        }
        //srapId	证券应收应付编号	varchar2
        if(securities.getSrapId()!=null){
            codition+="and srapId like '%"+securities.getSrapId()+"%'";
        }
        //businDate	业务日期	date
        if(securities.getBusinDate()!=null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            codition+="and businDate like to_date('"+sdf.format(securities.getBusinDate())+"','yyyy-MM-dd')";
        }
        //seId	证券信息编号	varchar2
        if(securities.getSeId()!=null && securities.getSeId().trim().length()!=0){
            codition+="and seId like '%"+securities.getSeId()+"%'";
        }

        //money	金额	number
        if(securities.getMoney()!=0){
            codition+="and money like '%"+securities.getMoney()+"%'";
        }

        return codition;
    }

    /**
     * 删除证券应收应付需要反冲销的数据
     * @return
     */
    @Override
    public Boolean washDelete() {
        try {
            //调用方法获得需要反冲销数据的id
            Map<String,String> wash=null;
            while(securitiesDao.washDelete()!=null && (wash=securitiesDao.washDelete()).size()!=0){
                for (String key : wash.keySet()) {
                    //调用删除现金应收应付的方法：根据id删除数据
                    securitiesDao.delete(wash.get(key));
                }
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
